/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.commons;

import com.badlogic.gdx.math.Vector2;

public class VectorRect {

    public final Vector2 pos;
    public final Size size;

    public VectorRect() {
        this(0, 0, 0, 0);
    }

    public VectorRect(final Vector2 pos, final Size size) {
        this(pos.x, pos.y, size.width, size.height);
    }

    public VectorRect(float x, float y, float width, float height) {
        pos = new Vector2(x, y);
        size = new Size(width, height);
    }


    public void set(float x, float y, float width, float height) {
        pos.set(x, y);
        size.set(width, height);
    }

    public void setPoints(float left, float top, float right, float bottom) {
        set(left, bottom, right - left, top - bottom);
    }

    public boolean fits(float width, float height) {
        return this.size.width >= width && this.size.height >= height;
    }

    public boolean matches(float width, float height) {
        return this.size.width == width && this.size.height == height;
    }

    public boolean contains(float x, float y) {
        return size.width > 0 && size.height > 0  // check for empty first
                && x >= pos.x && x < (pos.x + size.width)
                && y >= pos.y && y < (pos.y + size.height);
    }

    public float maxX() {
        return pos.x + size.width;
    }

    public float maxY() {
        return pos.y + size.height;
    }

    public float midX() {
        return pos.x + (float) (size.width / 2.0);
    }

    public float midY() {
        return pos.y + (float) (size.height / 2.0);
    }

    public float minX() {
        return pos.x;
    }

    public float minY() {
        return pos.y;
    }


    /**
     * Returns true if aPoint is inside aRect.
     */
    public static boolean containsPoint(final VectorRect aRect, final Vector2 aPoint) {
        return ((aPoint.x >= minX(aRect))
                && (aPoint.y >= minY(aRect))
                && (aPoint.x < maxX(aRect))
                && (aPoint.y < maxY(aRect)));
    }

    public static boolean containsRect(final VectorRect aRect, final VectorRect bRect) {
        return (!isEmptyRect(bRect)
                && (minX(aRect) <= minX(bRect))
                && (minY(aRect) <= minY(bRect))
                && (maxX(aRect) >= maxX(bRect))
                && (maxY(aRect) >= maxY(bRect)));
    }

    /**
     * Returns the greatest x-coordinate value still inside aRect.
     */
    public static float maxX(final VectorRect aRect) {
        return aRect.pos.x + aRect.size.width;
    }

    /**
     * Returns the greatest y-coordinate value still inside aRect.
     */
    public static float maxY(final VectorRect aRect) {
        return aRect.pos.y + aRect.size.height;
    }

    /**
     * Returns the x-coordinate of aRect's middle point.
     */
    public static float midX(VectorRect aRect) {
        return aRect.pos.x + (float) (aRect.size.width / 2.0);
    }

    /**
     * Returns the y-coordinate of aRect's middle point.
     */
    public static float midY(final VectorRect aRect) {
        return aRect.pos.y + (float) (aRect.size.height / 2.0);
    }

    /**
     * Returns the least x-coordinate value still inside aRect.
     */
    public static float minX(final VectorRect aRect) {
        return aRect.pos.x;
    }

    /**
     * Returns the least y-coordinate value still inside aRect.
     */
    public static float minY(final VectorRect aRect) {
        return aRect.pos.y;
    }

    /**
     * Returns aRect's width.
     */
    public static float width(final VectorRect aRect) {
        return aRect.size.width;
    }

    /**
     * Returns aRect's height.
     */
    public static float height(final VectorRect aRect) {
        return aRect.size.height;
    }


    public static boolean isEmptyRect(VectorRect aRect) {
        return (!(aRect.size.width > 0 && aRect.size.height > 0));
    }


}

